# -*- coding: UTF-8 -*-

import sys
import os
from time import sleep
#发送数据到send_onenet
import send_onenet
#显示屏驱动
from send_onenet import send_pic, post_json


#dlib处理
from img_pre import predo

#导入rqy的模型
from model_age_rqy import resnet50

#导入lzl的模型
from yolov5_lzl_model import yolov5_lzl_init

#导入zt的模型
from yolov5_zt_model import yolov5_zt_init

#json文件处理
from json_process import load_json

import datetime



from cameracapture import CameraCapture
from acllite_model import AclLiteModel
from acllite_resource import AclLiteResource
from acllite_imageproc import AclLiteImageProc

import cv2

#列表元素类型:string
model_path = []
model_path.append('../model/resnet50_age.om')

#ing_save_path : string类型
img_save_path=[]
img_save_path.append('../img/img_camera/camera_age.jpg')

#照片大小确定
CAMERA_FRAME_WIDTH = 1280
CAMERA_FRAME_HEIGHT = 720

#导入串口实现语音控制
from serial import Serial
from time import sleep


class audio_ctrl():

    def __init__(self):
        #串口具体位置
        self.ser = Serial("/dev/ttyAMA1", 115200, timeout=5)

    def receive(self):
        self.red = self.ser.readline()
        print(self.red)
        if self.red == b'camera_run\r\n':
            print('audio_success')
            self.ser.write(b'audio_success\r\n')
            return True
        return False

    def stop(self):
        self.red=self.ser.readline()
        if self.red == b'stop\r\n':
            print('stop_program')
            return True
        return False


def main():
    acl_resource = AclLiteResource()
    acl_resource.init()
    #用于解码编码图片
    dvpp = AclLiteImageProc(acl_resource)
    #摄像头通道camera_0
    cap = CameraCapture(0)
    

    oled=oled()
    audio = audio_ctrl()
    while True:
        #接收语音识别模块的串口数据
        rec = audio.receive()
        oled.show('Please start',(5,10))
        if rec == True:
            oled.show('Processing',(5,10))
            # Read a picture from the camera
            image_camera = cap.read()
            if image_camera is None:
                print("Get memory from camera failed")
                break

            image_camera = dvpp.jpege(image_camera)
            if image_camera is None:
                print("Pre process image failed")
                break
            image_camera.save(img_save_path[0])

            

            #图像预处理
            try:
                image=predo(r'../img/img_camera/camera_age.jpg').output2Fold()#赖志立的要求
                image_nose= predo(r'../img/img_camera/camera_age.jpg').gan()#钟涛的要求
                cv2.imwrite("../img/img_yolov5_lzl/1.jpg",image)
                print(image.shape)
                cv2.imwrite(r"../img/img_yolov5_zt/1.jpg",image_nose)
            except:
                oled.show('Fail, please do it again after 2s',(5,10))
                sleep(2)
                continue
                
            

            
            #模型处理
            resnet50(model_path[0],img_save_path[0])
            yolov5_lzl_init()
            yolov5_zt_init()


            #json处理
            acen_num,mole_num=load_json(r'../json/yolov5_lzl.json').show_time_person()
            nose_degree=load_json(r'../json/yolov5_zt.json').show_time_nose()
            #屏幕显示
            acen='acen:'+acen_num
            mole='mole:'+mole_num
            nose_degree='nose_degress:'+nose_degree
            oled.show(acen,(5,10))
            oled.show(mole,(5,20))
            oled.show(nose_degree,(5,30))
            
            #上传数据
            post_json('yolov5_lzl_id','../json/yolov5_lzl.json')
            post_json('yolov5_zt_id','../json/yolov5_zt.json')
            send_pic('../img/yolov5_lzl_post/1.jpg')




            #删除相关文件
            #os.remove()


if __name__ == '__main__':
    main()